package org.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.feng.entity.Menu;
import org.feng.mapper.MenuMapper;
import org.feng.service.MenuService;
import org.feng.service.UserService;
import org.feng.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private UserService userService;

    @Override
    public Boolean createBtn(List<Menu> list) {
        //查询菜单属性
        List<Long> idList = list.stream().map(Menu::getId).collect(Collectors.toList());
        List<Menu> menuList = baseMapper.selectBatchIds(idList);
        for (Menu menu : menuList) {
            if (menu.getCategory() == 1) {
                return false;
            }
        }
        //删除菜单下所有按钮
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.in("parent_id", idList);
        wrapper.eq("category", "1");
        baseMapper.delete(wrapper);
        //增加按钮
        for (Menu sysMenu : menuList) {
            String path = sysMenu.getPath();
            String code = sysMenu.getCode();
            //新增
            Menu menu = new Menu();
            menu.setCategory(1);
            menu.setName("新增");
            menu.setParentId(sysMenu.getId());
            menu.setSort(1);
            menu.setCode(code + "_add");
            menu.setPath(path + "/add");
            createBtn(menu);
            //查看
            menu.setId(null);
            menu.setName("查看");
            menu.setSort(2);
            menu.setCode(code + "_view");
            menu.setPath(path + "/view");
            createBtn(menu);
            //编辑
            menu.setId(null);
            menu.setName("编辑");
            menu.setSort(3);
            menu.setCode(code + "_edit");
            menu.setPath(path + "/edit");
            createBtn(menu);
            //删除
            menu.setId(null);
            menu.setName("删除");
            menu.setSort(4);
            menu.setCode(code + "_delete");
            menu.setPath(path + "/delete");
            createBtn(menu);
            //导出
            menu.setId(null);
            menu.setName("导出");
            menu.setSort(5);
            menu.setCode(code + "_excel");
            menu.setPath(path + "/excel");
            createBtn(menu);
            //打印
            menu.setId(null);
            menu.setName("打印");
            menu.setSort(6);
            menu.setCode(code + "_print");
            menu.setPath(path + "/print");
            createBtn(menu);

            //复制
            menu.setId(null);
            menu.setName("复制");
            menu.setSort(6);
            menu.setCode(code + "_copy");
            menu.setPath(path + "/copy");
            createBtn(menu);
        }
        return true;
    }

    @Override
    public List<Menu> treeList() {
        List<Menu> list = baseMapper.treeList(userService.getById(UserUtil.getUserId()));
        List<Menu> menuList = new ArrayList<>();
        list.forEach(menu -> {
            Menu parentMenu = baseMapper.selectById(menu.getParentId());
            if (null != parentMenu && !list.contains(parentMenu)) {
                menuList.add(parentMenu);
            }
        });
        list.addAll(menuList);
        return list;
    }

    @Override
    public List<Menu> getButtons(Long roleId) {
        return baseMapper.getButtons(roleId);
    }

    private void createBtn(Menu menu) {
        baseMapper.insert(menu);
    }

}
